package com.wontube.smr.bo.sites.imdb;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.wontube.client.extension.utils.ArrayUtils;
import com.wontube.client.extension.utils.StringUtils;
import com.wontube.smr.bo.constants.StandarMovieGenres;
import com.wontube.smr.client.model.po.Genre;

/**
 * @author tanggf(2012-7-27)
 *
 */
public class ImdbMovieGenres {	
	private ImdbMovieGenres(){}
	
	//private static final Map<String, String> siteStandarMap = new HashMap<String, String>();		
	private final static Map<String, String> map = new HashMap<String, String>();
	//使用youtube相同的I18n genres
	private final static List<Genre> i18nGenres = new ArrayList<Genre>();
	
	static {
		//loadMovieGenersMap();
		
		mapStandarGenres();
		initI18nGenres();
	}
	
	private static void mapStandarGenres(){
		//imdb需要修正的genre
		//Thriller, Sport, Sci-Fi, "Game-Show", "Musical", "Mystery"
		//Film-noir,Reality-TV
		map.put("Thriller", StandarMovieGenres.HORROR);
		map.put("Sport", StandarMovieGenres.SPORTS);
		map.put("Sci-Fi", StandarMovieGenres.SCIENCE);
		map.put("Game-Show", StandarMovieGenres.GAME);
		map.put("Musical", StandarMovieGenres.MUSIC);
		map.put("Mystery", StandarMovieGenres.HORROR);
		map.put("Film-Noir", StandarMovieGenres.FILMNOIR);
		map.put("Reality-TV", StandarMovieGenres.REALITY_TV);
		
		map.put("Western", StandarMovieGenres.OTHERS);
		map.put("Short", StandarMovieGenres.OTHERS);
		map.put("Adult", StandarMovieGenres.OTHERS);
		map.put("Talk-Show", StandarMovieGenres.OTHERS);
	}
	
	private static void initI18nGenres(){
		i18nGenres.add(new Genre(StandarMovieGenres.ACTION, "Action"));
		i18nGenres.add(new Genre(StandarMovieGenres.ANIMATION, "Animation"));
		i18nGenres.add(new Genre(StandarMovieGenres.ADVENTURE, "Adventure"));
		i18nGenres.add(new Genre(StandarMovieGenres.BIOGRAPHY, "Biography"));
		i18nGenres.add(new Genre(StandarMovieGenres.COMEDY, "Comedy"));
		i18nGenres.add(new Genre(StandarMovieGenres.CRIME, "Crime"));
		i18nGenres.add(new Genre(StandarMovieGenres.Documentary, "Documentary"));
		
		i18nGenres.add(new Genre(StandarMovieGenres.DRAMA, "Drama"));
		i18nGenres.add(new Genre(StandarMovieGenres.FAMILY, "Family"));
		i18nGenres.add(new Genre(StandarMovieGenres.HORROR, "Horror"));
		
		i18nGenres.add(new Genre(StandarMovieGenres.ROMANCE, "Romance"));
		i18nGenres.add(new Genre(StandarMovieGenres.SCIENCE, "Science"));
		
		i18nGenres.add(new Genre(StandarMovieGenres.OTHERS, "Others"));
	}
	
	/*
	private static void loadMovieGenersMap(){
		List<GenreSite> genreSites = GenreSelectDao.selectGenreSites(SiteConstants.WEBSITE_IMDB, 
				Genre.CATE_MOVIE);
		for (GenreSite gs : genreSites) {
			siteStandarMap.put(gs.getSiteGenre(), gs.getStandarGenre());
		}
	}
	*/
	
	public static List<Genre> getI18nGenres(){
		return i18nGenres;
	}
	
	public static String[] getI18nOtherGenres(){
		//TODO, 等待同步更新
		return new String[] {StandarMovieGenres.OTHERS, StandarMovieGenres.FANTASY, 
				StandarMovieGenres.FILMNOIR, StandarMovieGenres.HISTORY,
				StandarMovieGenres.MUSIC,  StandarMovieGenres.NEWS, 
				StandarMovieGenres.REALITY_TV, StandarMovieGenres.WAR};
	}
	
	
	/**
	 * 修正单个genre
	 * @param imdbGenre
	 * @return
	 */
	public static String remendGenre(String imdbGenre){
		if(StringUtils.isNullOrEmpty(imdbGenre)){
			return null;
		}
		
		//处理专有genre, 不存在则判断是标准genre
		imdbGenre = imdbGenre.trim();
		String genre = map.get(imdbGenre);
		return genre != null ? genre : imdbGenre;
	}
	
	/**
	 * 修正以逗号分隔的genres
	 * @param genreNames
	 * @return
	 */
	public static String remendAllGenres(String genreNames){
		if(StringUtils.isNullOrEmpty(genreNames)){
			return null;
		}
		
		Set<String> genreSet = new HashSet<String>();
		
		String[] genres = genreNames.split("\\s*,\\s*");
		for (String g : genres) {
			String rg = remendGenre(g);
			if(rg != null){
				genreSet.add(rg);
			}			
		}
		
		return ArrayUtils.join(genreSet, ",");
	}
}
